/**Created by the LayaAirIDE*/
module view.dlg{
	export class warehouse extends ui.dlg.warehouseUI{

		private now_rate : number;
		
		itemBtns : Array<Laya.Button> = [
			//各种按钮列表
			this.ur_btn,
			this.sr_btn,
			this.ssr_btn,
			this.item_btn,
			this.bgExit
		]

		constructor(){
			super();
			this.init();
		}

		initialize() : void{
			let scalex = Laya.stage.width / Laya.stage.designWidth;
    		let scaley = Laya.stage.height / Laya.stage.designHeight;
			this.scale(scalex, scaley);
		}

		private init()
		{
			//各种按钮列表事件
			for (var button of this.itemBtns)
			{
				let buttons : Array<string> = [button.name];
				button.clickHandler = new Laya.Handler(this, this.addBtnClickEvent, buttons);
			}

			this.setScrollView(Cfg.rateLevel["ur"]);
		}

		//相应按钮事件，以name分开
		private addBtnClickEvent(name : string)
		{
			switch (name)
			{
				case "ur" :
					this.reqItemList(1);
					break;
				case "sr" :
					this.reqItemList(2);
					break;
				case "ssr" :
					this.reqItemList(3);
					break;
				case "item" :
					this.reqItemList(0);
					break;
				case "exit" :
					CtrlManager.GetCtrl(CtrlNames.Warehouse).CloseDlg();
					break;
				default:
					break;
			}
		}

		private reqItemList(idx : number)
		{
			//请求道具列表
			this.setScrollView(idx);
		}

		public setItemList(data : any)
		{

		}

		private setScrollView(idx : number)
		{
			//设置一下按钮表状态
			this.now_rate = idx;

			for (var button of this.itemBtns)
			{
				if (button.name == Cfg.rateLevel[idx])
				{
					button.skin = "main/wh_btn_true.png";
				} 
				else
				{
					button.skin = "main/wh_btn_false.png";
				}
			}

			//item属性获取
			let itemDatas : Array<ItemData> = this.getItemDataFormRate(idx);

			// this.scrollview.hScrollBarSkin = '';

			//动态设置ScrollView属性（比如重复多少次repeatX，每个item的属性array，以及渲染事件）
			// this.scrollview.repeatX = 10;
			this.scrollview.array = itemDatas; //必须有array属性才是一个ScrollView
			this.scrollview.renderHandler = new Laya.Handler(this, this.onSVRender1);
		}

		private getItemDataFormRate(rate : number) : Array<ItemData>
		{
			//获取Item数据
			switch (rate)
			{
				case 0 :
					return WarehouseModel.GetItemList();
				case 1 :
					return WarehouseModel.GetUrSeedList();
				case 2 :
					return WarehouseModel.GetSrSeedList();
				case 3 :
					return WarehouseModel.GetSsrSeedList();
				default :
					break;
			}
		}

		private onSVRender1(cell : Laya.Box, index : number)
		{
			var item_img : Laya.Image = cell.getChildByName("seed_img") as Laya.Image;
			let labItemNum = cell.getChildByName("labItemNum") as Laya.Label;
			var item_btn : Laya.Button = cell.getChildByName("seed_btn") as Laya.Button;
			
			let data : ItemData = this.scrollview.array[index];
			let args : Array<ItemData> = [data];

			//对图像单独进行设置
			item_img.skin = "item/" + data.itemSkin + ".png";
			
			//对数量clip图进行赋值
			var temp : number = data.itemNum;
			item_btn.offAll(Laya.Event.CLICK);
			if (temp == 0)
			{
				item_img.gray = true;
			} else
			{
				item_img.gray = false;
				item_btn.on(Laya.Event.CLICK, this, this.addItemClickEvent,args);
			}
			labItemNum.text = "x" + data.itemNum.toString();
			if (data.itemNum > 0 ) {
				labItemNum.color = "#666666";
			} else {
				labItemNum.color = "#AAAAAA";
			}
			item_btn.on(Laya.Event.MOUSE_DOWN, this, this.showDesTimer,[data,item_btn]);
			item_btn.on(Laya.Event.MOUSE_UP, this, this.hideDesTimer,[data,cell,index]);
			item_btn.on(Laya.Event.MOUSE_OUT, this, this.hideDesTimer,[data,cell,index]);
		}

		private showDesTimer(item : ItemData,item_btn) : void{
			Laya.timer.once(500,this,this.showDes,[item,item_btn]);
		}

		private showDes(item : ItemData,item_btn){
			if(item.itemRate == Cfg.rateLevel["item"]){
				this.itemDes.getChildByName("desName")["text"] = item.itemName;
				this.itemDes.getChildByName("desTime")["text"] = Cfg.ItemCfg[item.itemId]["depict"];
				this.itemDes.getChildByName("desScore")["text"] = "";
				this.itemDes.getChildByName("desMreward")["text"] = "";
			}else{
				this.itemDes.getChildByName("desName")["text"] = item.itemName;
				this.itemDes.getChildByName("desTime")["text"] = "成熟时间: " + ComFun.formatTiming(item.itemTime * 1000, "d天h小时m分s秒", false);
				this.itemDes.getChildByName("desScore")["text"] = "预计积分: " + item.itemScore;
				this.itemDes.getChildByName("desMreward")["text"] = "最低收成: " + item.itemMreward;
			}
			this.itemDes.visible = true;
			item_btn.offAll(Laya.Event.CLICK);
		}

		private hideDesTimer(item : ItemData, cell,index){
			Laya.timer.clear(this,this.showDes);
			if(this.itemDes.visible){
				this.itemDes.visible = false;
				Laya.timer.once(500,this,this.onSVRender1,[cell,index]);
			}
		}

		private addItemClickEvent(data : ItemData)
		{
			if (data.itemNum > 0){
				if(data.itemRate == Cfg.rateLevel.item){
					CtrlManager.GetCtrl(CtrlNames.Warehouse)[Cfg.ItemCfg[Cfg.ItemCfg[data.itemId]["prop_type"]]["value"]](data.itemId, data.itemNum);
				}else{
					CtrlManager.GetCtrl(CtrlNames.Field).Plant(data.itemId);
				}
			}
		}

		private addMouseDownEvent(button : Laya.Button, index : number)
		{
			button.on(Laya.Event.MOUSE_DOWN, button, function(){
			});
		}

		public ResetScrollView()
		{
			this.setScrollView(this.now_rate);
		}

		public SetNowRate(data)
		{
			if (typeof(data[0]) == "number")
			{
				if (data[0] <= 4 && data[0] >= 0)
				{
					this.now_rate = data[0];
				}
			}
		}

		onClosed() {
			CtrlManager.GetCtrl(CtrlNames.Field).ClearSelected();
		}
	}
}